package com.kaizzi.data.ppt.template;

import com.fasterxml.jackson.databind.JsonNode;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.jdl.service.LogService;
import com.kaizzi.data.ppt.TemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.sl.usermodel.TableCell;
import org.apache.poi.sl.usermodel.TableShape;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.awt.*;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/27
 */
@Component
@Slf4j
public class Template17 {

    @Resource
    private TemplateUtil templateUtil;
    @Resource
    private LogService logService;

    public void text(List<XSLFTextParagraph> textParagraphs,JsonNode data) {
        if(textParagraphs==null||textParagraphs.size()==0){
            return;
        }
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            if(textParagraph==null){
                continue;
            }
            List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
            if(textRuns==null){
                continue;
            }
            for (XSLFTextRun textRun : textRuns) {
                if (textRun == null || textRun.getXmlObject()  == null){
                    return;
                }
                try {
                    if (textRun.getRawText().equals("17KEY-1")){
                        String title = data.get("title").asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("17KEY-3")){
                        String title = data.get("summary").get(0).asText();
                        textRun.setText(title);
                        break;
                    }
                    if (textRun.getRawText().equals("17KEY-4")){
                        if (data.get("summary").size()<1){
                            textRun.setText("");
                            break;
                        }
                        String title = data.get("summary").get(1).asText();
                        textRun.setText(title);
                        break;
                    }
                    if (textRun.getRawText().equals("17KEY-5")){
                        if (data.get("summary").size()<2){
                            textRun.setText("");
                            break;
                        }
                        String title = data.get("summary").get(2).asText();
                        textRun.setText(title);
                        break;
                    }
                    if (textRun.getRawText().equals("17KEY-6")){
                        if (data.get("summary").size()<3){
                            textRun.setText("");
                            break;
                        }
                        String title = data.get("summary").get(3).asText();
                        textRun.setText(title);
                        break;
                    }
                    if (textRun.getRawText().equals("17KEY-7")){
                        if (data.get("actionPlan").size()<1){
                            textRun.setText("");
                            break;
                        }
                        String title = data.get("actionPlan").get(0).asText();
                        textRun.setText(title);
                        break;
                    }
                    if (textRun.getRawText().equals("17KEY-8")){
                        if (data.get("actionPlan").size()<2){
                            textRun.setText("");
                            break;
                        }
                        String title = data.get("actionPlan").get(1).asText();
                        textRun.setText(title);
                        break;
                    }
                    if (textRun.getRawText().equals("17KEY-9")){
                        String title = data.get("dataPeriod").asText();
                        textRun.setText(title);
                        break;
                    }
                }catch (Exception e){
                    logService.setDataLogStatus("ppt-error");
                    log.error("page:{},text异常情况:{}",14,e);
                }
            }
        }
    }

    public void table(XSLFShape shape, JsonNode data) {
        TableShape tableShape = (TableShape)shape;
        int numberOfColumns = tableShape.getNumberOfColumns();
        int numberOfRows = tableShape.getNumberOfRows();

        Color color1 = new Color(192, 0, 0);
        Color color2 = new Color(0, 176, 80);

        //遍历获取每一个单元格
        for (int col =2 ;col < numberOfColumns; col++){
            for (int row = 0; row<numberOfRows; row++ ){
                TableCell cell = tableShape.getCell(row, col);
                if (cell.getTextParagraphs().size() > 0){

                    if (row == 0) {
//                        if (col ==2){
//                            XSLFTextParagraph cellParagraphs = (XSLFTextParagraph)cell.getTextParagraphs().get(0);
//                            XSLFTextRun textRun = cellParagraphs.getTextRuns().get(0);
//                            textRun.setText(DateUtil.getUsDate2());
//                            continue;
//                        }
                        if (col == 2){
                            XSLFTextParagraph cellParagraphs = (XSLFTextParagraph)cell.getTextParagraphs().get(0);
                            XSLFTextRun textRun = cellParagraphs.getTextRuns().get(0);
                            textRun.setText(DateUtil.getUsDate1());
                            continue;
                        }
                        continue;
                    }
                    if (row ==1){
//                        if (col == 2){
//                            XSLFTextParagraph cellParagraphs = (XSLFTextParagraph)cell.getTextParagraphs().get(0);
//                            XSLFTextRun textRun = cellParagraphs.getTextRuns().get(0);
//                            textRun.setText(DateUtil.getNumDate1());
//                            continue;
//                        }
                        if (col == 5){
                            continue;
                        }
                        if (col == 4){
                            continue;
                        }
                        if (col ==2){
                            XSLFTextParagraph cellParagraphs = (XSLFTextParagraph)cell.getTextParagraphs().get(0);
                            XSLFTextRun textRun = cellParagraphs.getTextRuns().get(0);
                            textRun.setText(DateUtil.getNumDate());
                            continue;
                        }
//                        continue;
                    }
                    //固定斜杠 不计算 2020/11/12
                    if (row == 2){
                        if (col == 5){
                            continue;
                        }
                    }

                    if (row == 7 || row ==11){
                        if (col == 4 ){
                            continue;
                        }
                    }

                    XSLFTextParagraph cellParagraphs = (XSLFTextParagraph)cell.getTextParagraphs().get(0);
                    XSLFTextRun textRun = cellParagraphs.getTextRuns().get(0);

//                    if (col == 3){
//
//                        String thisCell = getCellData(data, col, row);
//                        String beforeCell = getCellData(data,col-1,row);
//                        if (thisCell.equals("-")||beforeCell.equals("-")){
//                            textRun.setText(thisCell);
//                            continue;
//                        }
//                        if (row == 7 || row == 11){
//                            thisCell = thisCell.replace("%","");
//                            beforeCell = beforeCell.replace("%","");
//                        }
//                        if (thisCell.equals("/")||thisCell.equals("")){
//                            continue;
//                        }
//                        if (beforeCell.equals("/")||beforeCell.equals("")){
//                            continue;
//                        }
//                        double thisa = Double.parseDouble(thisCell.replace(",", "").replace("%",""));
//                        double beforea = Double.parseDouble(beforeCell.replace(",", "").replace("%",""));
//                        if (beforea  < thisa){
//                            textRun.setBold(true);
//                            textRun.setFontColor(color2);
//                        }
//                        if (beforea > thisa){
//                            textRun.setBold(true);
//                            textRun.setFontColor(color1);
//                        }
//                    }

                    if (col == 4){
                        String cellData = getCellData(data, col, row);
                        if (cellData.equals("-")){
                            textRun.setText(cellData);
                            continue;
                        }
                        double thisCell = 0;
                        try {
                            thisCell = Double.parseDouble(cellData.replace("%", "").replace(",",""));
                        }catch (Exception e){
//                            logService.setDataLogStatus("ppt-error");
//                            log.error("error",e);
                        }
                        if (thisCell == 0){
                            continue;
                        }
                        if (thisCell > 100){
                            textRun.setBold(true);
                            textRun.setFontColor(color2);
                        }
                        if (thisCell < 100){
                            textRun.setBold(true);
                            textRun.setFontColor(color1);
                        }
                    }

                    if (col == 4){
                        String cellData = getCellData(data, col, row);
                        if (!cellData.equals("-")){
                            textRun.setText(cellData);
                            continue;
                        }
                    }
                    if (col == 5){
                        String cellData = getCellData(data, col, row);
                        if (cellData.equals("-")){
                            textRun.setText(cellData);
                            continue;
                        }
                        double thisCell = 0;
                        try {
                            thisCell = Double.parseDouble(cellData.replace("%", "").replace(",",""));
                        }catch (Exception e){
//                            logService.setDataLogStatus("ppt-error");
//                            log.error("error",e);
                        }
                        if (thisCell == 0){
                            textRun.setText("/");
                            continue;
                        }

                        if (thisCell > ((double) DateUtil.getLastSunday().getDayOfMonth()/30*100)){
                            textRun.setText("√");
                            continue;
                        }else {
                            textRun.setBold(true);
                            textRun.setFontColor(color1);
                            textRun.setText("!");
                            continue;
                        }
                    }
                    textRun.setText(getCellData(data,col,row));
                }
            }
        }
    }

    private String getCellData(JsonNode data, int col, int row) {

        JsonNode jsonNode = data.get("page17s");
        String jsonKey = "thisKpi";
//        if (col == 2 || col == 3 || col == 4){
//            jsonKey = "lastKpi";
//        }else {
//            jsonKey = "thisKpi";
//        }

        String jsonKey1;
        switch (col){
            case 2:
                jsonKey1 = "targetKpi";
                break;
            case 3:
                jsonKey1 = "realKpi";
                break;
            case 4:
                jsonKey1 = "rate";
                break;
            case 5:
                jsonKey1 = "rate";//使用rate进行比较即可
                break;
//            case 6:
//                jsonKey1 = "realKpi";
//                break;
//            case 7:
//                jsonKey1 = "rate";
//                break;
            default:
                jsonKey1 = "";
                break;
        }


        String cellName;
        switch (row){
            case 2:
                cellName = "累计会员用户数";
                break;
            case 3:
                cellName = "当月新增会员数";
                break;
            case 4:
                cellName = "当月激活的当月注册的新用户";
                break;
            case 5:
                cellName = "当月召回老用户";
                break;
            case 6:
                cellName = "当月激活用户(e=c+d)";
                break;
            case 7:
                cellName = "当月用户总激活率（f=e/a)";
                break;
            case 8:
                cellName = "UTC渠道新增用户数";
                break;
            case 9:
                cellName = "拉新人数（当月新注册并参与一键组队的用户）";
                break;
            case 10:
                cellName = "召回老用户数（注册时间小于当月且在当月有一键组队行为）";
                break;
            case 11:
                cellName = "激活率*";
                break;
            default:
                cellName = "";
                break;

        }

        for (JsonNode node : jsonNode) {
            if (node.get("name").asText().equals(cellName)){
                String s = node.get(jsonKey).get(jsonKey1).asText();
                if (s.equals("") || s.equals("null")){
                    return "/";
                }
                return s;
            }
        }
        return "";
    }
}
